﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmNguyenLieu : Form
    {
        private NguyenLieuService httService = new NguyenLieuService();

        public void LoadThongTin(DataTable dbtab)
        {
            lv_NL.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[4].ToString());
                lv_NL.Items.Add(liv);
            }
        }
        public frmNguyenLieu()
        {
            InitializeComponent();
        }

        private void frmNguyenLieu_Load(object sender, EventArgs e)
        {
            DataTable dbtab = httService.LayNguyenLieu();
            LoadThongTin(dbtab);
            LoadDVTinh();
            LoadLoaiNL();
            LoadNCC();
        }

        private void lv_NL_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (lv_NL.FocusedItem != null)
                {
                    int index = lv_NL.FocusedItem.Index;
                    int ma = int.Parse(lv_NL.Items[index].Text.ToString());
                    DataTable dbtab = httService.LayNguyenLieuTheoMa(ma);
                    DataRow dbrow = dbtab.Rows[0];
                    txt_TenNL.Text = dbrow[1].ToString();
                    cmbNCC.SelectedValue = dbrow[2].ToString();
                    cmbLoaiNL.SelectedValue =dbrow[4].ToString();
                    cmbDVT.SelectedValue = dbrow[3].ToString();
                }
            }
            catch (Exception ex)
            {
                return;
                throw ex;
            }
        }

        private void LoadNCC()
        {
            NhaCungCapService NCC = new NhaCungCapService();
            cmbNCC.DisplayMember = "TenNCC";
            cmbNCC.ValueMember = "MaNCC";
            cmbNCC.DataSource = NCC.LoadTatCaNhaCungCap();
        }

        private void LoadDVTinh()
        {
            LoaiNguyenLieuService DVT = new LoaiNguyenLieuService();
            cmbDVT.DisplayMember = "TenDVTinh";
            cmbDVT.ValueMember = "MaDVTinh";
            cmbDVT.DataSource = DVT.LoadDonViTinh();
        }

        private void LoadLoaiNL()
        {
            LoaiNguyenLieuService LoaiNL = new LoaiNguyenLieuService();
            cmbLoaiNL.DisplayMember = "TenLoaiNL";
            cmbLoaiNL.ValueMember = "MaLoaiNL";
            cmbLoaiNL.DataSource = LoaiNL.LoadLoaiNguyenLieu();
        }

        private void cmbDVT_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maDVT = int.Parse(cmbDVT.SelectedValue.ToString());
            DataTable dbtab = httService.LayNguyenLieuTheoDVT(maDVT);
            LoadThongTin(dbtab);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            String tenNL = txt_TenNL.Text.ToString();
            //MessageBox.Show(cmbLoaiNL.SelectedValue.ToString());
            if (tenNL != "")
            {
                int maNCC = int.Parse(cmbNCC.SelectedValue.ToString());
                int maDVT = int.Parse(cmbDVT.SelectedValue.ToString());
                int maLoaiNL = int.Parse(cmbLoaiNL.SelectedValue.ToString());
                if (!httService.ThemNguyenLieu(tenNL, maNCC, maDVT, maLoaiNL))
                {
                    MessageBox.Show("Thêm Không Thành Công");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Tên Nguyên Liệu Rỗng");
            }
            DataTable dbtab = httService.LayNguyenLieu();
            LoadThongTin(dbtab);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (lv_NL.FocusedItem != null)
            {
                int index = lv_NL.FocusedItem.Index;
                int ma = int.Parse(lv_NL.Items[index].Text.ToString());
                if (!httService.XoaNguyenLieu(ma))
                {
                    MessageBox.Show("Xóa Không Thành Công");
                    return;
                }
                DataTable dbtab = httService.LayNguyenLieu();
                LoadThongTin(dbtab);
            }
            else
            {
                MessageBox.Show("Chưa chọn dữ liệu xóa");
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (lv_NL.FocusedItem != null)
            {
                string ten = txt_TenNL.Text;
                int maNCC = int.Parse(cmbNCC.SelectedValue.ToString());
                int maDVTinh = int.Parse(cmbDVT.SelectedValue.ToString());
                int maLoaiNL = int.Parse(cmbLoaiNL.SelectedValue.ToString());
                if (ten != "")
                {
                    int index = lv_NL.FocusedItem.Index;
                    int ma = int.Parse(lv_NL.Items[index].Text.ToString());
                    if (!httService.SuaNguyenLieu(ma,ten,maNCC,maDVTinh,maLoaiNL))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        return;
                    }
                    DataTable dbtab = httService.LayNguyenLieu();
                    LoadThongTin(dbtab);
                }
                else
                {
                    MessageBox.Show("Không Thể để trống");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn nguyên liệu");
                return;
            }
        }

        private void cmbNCC_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNCC = int.Parse(cmbNCC.SelectedValue.ToString());
            DataTable dbtab = httService.LayNguyenLieuTheoNCC(maNCC);
            LoadThongTin(dbtab);
        }

        private void cmbLoaiNL_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maLoaiNL = int.Parse(cmbLoaiNL.SelectedValue.ToString());
            DataTable dbtab = httService.LayNguyenLieuTheoLoai(maLoaiNL);
            LoadThongTin(dbtab);
        }

        private void lv_NL_Click(object sender, EventArgs e)
        {
            //DataTable dbtab = httService.LayNguyenLieu();
            //LoadThongTin(dbtab);
        }

        private void lv_NL_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            DataTable dbtab = httService.LayNguyenLieu();
            LoadThongTin(dbtab);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}
